package com.weather.androidweather.service;

import android.app.NotificationChannel;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.os.Build;
import androidx.core.app.NotificationCompat;
import com.weather.androidweather.MainActivity;
import com.weather.androidweather.R;
import com.weather.androidweather.model.WeatherData;

public class WeatherAlertService {
    private static final String CHANNEL_ID = "weather_alert_channel";
    private static final int NOTIFICATION_ID = 1;
    private final Context context;
    private final NotificationManager notificationManager;

    public WeatherAlertService(Context context) {
        this.context = context;
        this.notificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
        createNotificationChannel();
    }

    private void createNotificationChannel() {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            NotificationChannel channel = new NotificationChannel(
                CHANNEL_ID,
                "天气预警",
                NotificationManager.IMPORTANCE_HIGH
            );
            channel.setDescription("天气预警通知");
            notificationManager.createNotificationChannel(channel);
        }
    }

    public void checkWeatherAlert(WeatherData weatherData) {
        if (weatherData == null) return;

        String alertMessage = null;
        String weatherCondition = weatherData.getWeatherCondition();
        double temperature = weatherData.getTemperature();

        // 检查天气状况
        if (weatherCondition.contains("暴雨") || weatherCondition.contains("大暴雨")) {
            alertMessage = "暴雨预警：请注意防范强降雨可能带来的灾害";
        } else if (weatherCondition.contains("大雪") || weatherCondition.contains("暴雪")) {
            alertMessage = "暴雪预警：请注意防范积雪和道路结冰";
        } else if (weatherCondition.contains("台风")) {
            alertMessage = "台风预警：请注意防范大风和强降雨";
        } else if (weatherCondition.contains("雷电")) {
            alertMessage = "雷电预警：请注意防范雷击风险";
        } else if (weatherCondition.contains("冰雹")) {
            alertMessage = "冰雹预警：请注意防范冰雹灾害";
        } else if (weatherCondition.contains("沙尘暴")) {
            alertMessage = "沙尘暴预警：请注意防范沙尘天气";
        } else if (weatherCondition.contains("雾")) {
            alertMessage = "大雾预警：请注意交通安全";
        }

        // 检查温度
        if (temperature >= 35) {
            alertMessage = "高温预警：请注意防暑降温";
        } else if (temperature <= -10) {
            alertMessage = "低温预警：请注意防寒保暖";
        }

        // 如果有预警信息，显示通知
        if (alertMessage != null) {
            showNotification(alertMessage);
        }
    }

    private void showNotification(String message) {
        Intent intent = new Intent(context, MainActivity.class);
        intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
        PendingIntent pendingIntent = PendingIntent.getActivity(context, 0, intent, PendingIntent.FLAG_IMMUTABLE);

        NotificationCompat.Builder builder = new NotificationCompat.Builder(context, CHANNEL_ID)
            .setSmallIcon(R.drawable.ic_alert)
            .setContentTitle("天气预警")
            .setContentText(message)
            .setPriority(NotificationCompat.PRIORITY_HIGH)
            .setContentIntent(pendingIntent)
            .setAutoCancel(true);

        notificationManager.notify(NOTIFICATION_ID, builder.build());
    }
} 